<?php

/* Login functie */
function login($userName, $Password)
{
	//echo('login('.$userName.', '.$Password.')';
	
	global $wsdl;
	global $session_id;

	$sugar_client = new nusoapclient($wsdl, true);

	$auth_array = array(
					'user_auth' => array(
						'user_name' => $userName,
						'password' => md5($Password))
	 		      	  );

	
	$login_results =  $sugar_client->call('login', $auth_array);
	
	if($login_results['error']['name']=="No Error")
	{
         $session_id =  $login_results['id'];
         $_SESSION['session'] = $session_id;
		 
		 $user_guid = $sugar_client->call('get_user_id', $session_id);
		 
		 $set_entry_params = array(
								  'session' => $session_id,
								  'module_name' => 'Roles',
								  'query' => 'acl_roles.id = acl_roles_users.role_id AND acl_roles_users.user_id = \''.$user_guid.'\'',
								  'order_by' => '',
								  'select_fields'=>array(
															'id',
															'name'
														),
								  'deleted' => false
								  );
		 
		 $result = $sugar_client->call('get_entry_list', $set_entry_params);
		 
		 if($result['result_count'] == 0)
		 {
			 $_SESSION['Rol'] = 'Admin';
		 }
		 elseif($result['result_count'] == 1)
		 {
			 foreach($result['entry_list'] as $record)
			 {
				$array = nameValuePairToSimpleArray($record['name_value_list']);
				$_SESSION['Rol'] = $array['name'];
			 }
		 }
		 else
		 {
			 foreach($result['entry_list'] as $record)
			 {
				 $array = nameValuePairToSimpleArray($record['name_value_list']);
				 if($array['name'] == 'Student')
				 {
					 $_SESSION['Rol'] = $array['name'];
				 }
				 if($array['name'] == 'Docent')
				 {
					 $_SESSION['Rol'] = $array['name'];
				 }
				 if($array['name'] == 'Administratie')
				 {
					 $_SESSION['Rol'] = $array['name'];
				 }
				 if($array['name'] == 'StageCoordinator')
				 {
					 $_SESSION['Rol'] = $array['name'];
				 }
			 }
		 }
		 
		 $set_entry_params = array(
								  'session' => $session_id,
								  'module_name' => 'Users',
								  'id' => $user_guid,
								  'select_fields'=>array(
															'id',
															'reports_to_id'
														)
								  );
		 
		 $result = $sugar_client->call('get_entry', $set_entry_params);
		 $student_id ='';
		 $relatie_id ='';
		 
		 foreach($result['entry_list'] as $record)
		 {
			 $array = nameValuePairToSimpleArray($record['name_value_list']);
			 $relatie_id = $array['reports_to_id'];
			 break;
		 }
		 
		 $rol = $_SESSION['Rol'];
		 
		 if($rol == 'Admin')
		 {
			 $_SESSION['name'] = $rol;
		 }
		 elseif($rol == 'Administratie')
		 {
			 $_SESSION['name'] = $rol;
		 }
		 elseif($rol == 'Student')
		 {
		 	 $getStudent_obj = new GetStudent($wsdl);
			 print_r($getStudent_ob);

		 	 $student = $getStudent_obj->getStudent($relatie_id);
			 
			 
		 	 $_SESSION['name'] = $student->getFullName();
			 $_SESSION['id'] = $student->getId();
		 }
		 elseif($rol == 'Docent')
		 {
			 $getDocent = new GetDocent($wsdl);
			 $docent = $getDocent->getDocent($relatie_id);
		 	 $_SESSION['name'] = $docent->getFullName();
			 $_SESSION['id'] = $docent->getId();
		 }
		 elseif($rol == 'StageCoordinator')
		 {
			 //TODO: Veranderen naar hoe het eigenlijk zou moeten
			 $_SESSION['name'] = 'StageCoordinator';
		 }
		 		 
         return $session_id;
	}
	else
	{
         return $login_results['error']['name'];
	}
}

?>